package com.csxy.mxj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.csxy.mxj.R;
import com.csxy.mxj.entity.TOrder;
import com.csxy.mxj.exception.MyException;
import com.csxy.mxj.service.OrderService;
import com.csxy.mxj.util.JwtUtils;
import io.micrometer.common.util.StringUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/edeorder/order")
public class OrderController {
    @Autowired
    public OrderService orderService;
    //生成订单的方法
    @PostMapping("/createOrder/{courseId}")
    public R createOrder(@PathVariable("courseId") String courseId, HttpServletRequest request){
        //从请求头中获取用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //判断是否登录
        if(StringUtils.isEmpty(memberId)){
            throw new MyException(20001,"请登录");
        }
        //生成订单，并生成对应的订单号
        String orderNo = orderService.createOrders(courseId, memberId);
        return R.ok().data("orderId",orderNo);
    }
    //根据订单id查询订单信息
    @GetMapping("/getOrderInfo/{orderNo}")
    public R getOrderInfoById(@PathVariable("orderNo") String orderNo){
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderNo);
        TOrder tOrder = orderService.getOne(wrapper);
        return R.ok().data("item",tOrder);
    }

}
